//登录以及账号管理
import request from "@/api/request.js";
import { useUserStore } from "@/stores/modules/user.ts";
const currentUserId = useUserStore().userInfo.userId;

//添加账号
export const addAccountService = DataObj => {
  const params = new URLSearchParams();
  for (let key in DataObj) {
    params.append(key, DataObj[key]);
  }
  const queryString = params.toString();
  let url = `/api-self/users/account/register?${queryString}`;
  return request.post(url);
};

//登录接口
export const userLoginService = loginData => {
  const params = new URLSearchParams();
  for (let key in loginData) {
    params.append(key, loginData[key]);
  }
  return request.post("/api-self/users/account/adminLogin", params);
};

//get all
export const getAllWorkerService = () => {
  return request.get("/api-self/users/account/getUsers");
};

//edit
export const editWorkerService = dataObj => {
  const params = new URLSearchParams();
  for (let key in dataObj) {
    params.append(key, dataObj[key]);
  }
  // 将 params 转换为查询字符串
  const queryString = params.toString();
  // 构建完整的 URL
  const url = `/api-self/users/account/fixAccount?${queryString}`;
  return request.put(url);
};

//del
export const delWorkerService = dataObj => {
  const params = new URLSearchParams();
  for (let key in dataObj) {
    params.append(key, dataObj[key]);
  }
  let queryString = params.toString();
  let url = `/api-self/users/account/deleteUser?${queryString}`;
  return request.delete(url);
};

//search
export const searchWorkerService = dataObj => {
  const params = new URLSearchParams();
  for (let key in dataObj) {
    params.append(key, dataObj[key]);
  }
  // 将 params 转换为查询字符串
  const queryString = params.toString();
  // 构建完整的 URL
  const url = `/api-self/users/account/getUserInfo?${queryString}`;
  return request.get(url);
};

//修改密码
export const editPasswordService = dataObj => {
  return request.put("/api-self/users/account/fixPassword", dataObj, { params: { userId: currentUserId } });
};
